package com.xmlt.ui.xmltutils;

import java.util.List;

import com.xmlt.core.template.contentgenerators.IContentGenerator;
import com.xmlt.core.template.instance.AbstractInstance;
import com.xmlt.core.template.instance.Instance;
import com.xmlt.core.template.instance.MultiInstance;
import com.xmlt.core.template.instance.Value;
import com.xmlt.core.template.types.IChoiceValidator;
import com.xmlt.core.template.types.IValidator;

public class DisplayGenerator implements IContentGenerator {
	private Instance instance;
	public DisplayGenerator(Instance inst) {
		instance = inst;
	}
	
	protected Instance getInstance() {
		return instance;
	}
	
	public String getDisplayString() {
		String displayString = getDisplayString(instance);
		return displayString;
	}
	
	protected final String getDisplayString(AbstractInstance inst) {
		if (inst instanceof Value) {
			return getDisplayString((Value) inst);
		} else if (inst instanceof MultiInstance) {
			return getDisplayString((MultiInstance) inst);
		} else if (inst instanceof Instance) {
			return getDisplayString((Instance) inst);
		}
		return "";
	}
	
	protected String getDisplayString(Value value) {
		String displayValue = value.getDisplayValue();
		String contentValue = String.valueOf(value.getValue());
		displayValue = displayValue == null? "" : displayValue;
		IValidator validator = value.getValidator();
		if (validator instanceof IChoiceValidator) {
			String s = "<select id='" + value.getPath()
			+ "' onchange='setFieldValue(this);' "
			+ " class='" + (((Value) value).validate() ? "" : "error")
			+ "'>";
			for (String option : ((IChoiceValidator) validator).getChoices()) {
				s += "<option value='" + option + "' "
						+ (option.equals(contentValue) ? "selected" : "") + ">"
						+ validator.getDisplayValue(option) + "</option>";
			}
			s += "</select>";
			return s;
		} else {
			return "<input id='" + value.getPath()
					+ "' onchange='setFieldValue(this);' value='" + displayValue
					+ "' class='" + (((Value) value).validate() ? "" : "error")
					+ "'/><br/>";
		}
	}
	
	protected String getDisplayString(MultiInstance inst) {
		String path = inst.getPath();
		List<AbstractInstance> children = ((MultiInstance) inst).getChildren();
		String s = "<table id ='table_" + path + "' class='multiInstanceTable loop'><tbody>";
		s += "<tr><td colspan='2'><a id='" + path
				+ "' href='#' onclick='addListItem(this)'>add " + path
				+ "</a><br/></td></tr>";
		for (int i = 0; i < children.size(); i++) {
			s += "<tr><td>" + getDisplayString(children.get(i)) + "</td><td>";
			String childPath = path + "[" + i + "]";
			s += "<td><a id='" + childPath + "' href='#' onclick='delListItem(this)'>delete " + childPath + "</a></td></tr>";
		}
		s += "</tbody></table>";
		return s;
	}
	
	protected String getDisplayString(Instance inst) {
		String path = inst.getPath();
		String s = "<span id ='div_" + path + "' class=''>";
		List<String> names = inst.getChildrenNames();
		for (String n: names) {
			s += getDisplayString(inst.lookup(n));
		}
		s += "</span>";
		return s;
	}

	@Override
	public Class<?> getTargetClass() {
		return String.class;
	}

	@Override
	public Object getContent(Instance instance) {
		return getDisplayString(instance);
	}
}
